package com.yunsom.testdemo.multiple_thread.create_functions;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author Xbao
 * @description
 * @date 2022/5/10 10:34
 */
public class ImplementsCallableTest implements Callable<Integer> {

  public static void main(String[] args) {
    ImplementsCallableTest callableTest = new ImplementsCallableTest();
    FutureTask<Integer> futureTask = new FutureTask<Integer>(callableTest);
    for (int i = 0; i < 100; i++) {
      System.out.println(Thread.currentThread().getName() + "的循环变量i的值：" + i);
      if(i == 20){
        new Thread(futureTask, "有返回值的线程").start();
      }
    }

    try {
      System.out.println("子线程的返回值：" + futureTask.get());
    } catch (Exception e){
      e.printStackTrace();
    }

  }

  @Override
  public Integer call() {
    int i;
    for (i = 0; i < 100; i++) {
      System.out.println(Thread.currentThread().getName() + " " + i);
    }
    return i;
  }

}
